﻿using Developer.Core.FrameWork.DynamicMiddlePlatformService.Service;
using Developer.Core.FrameWork.HelpFile;
using Developer.Core.FrameWork.MiddlePlatformService.Options;
using Developer.Core.FrameWork.MiddlePlatformService.Service;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Developer.Core.FrameWork.DynamicMiddlePlatformService.Impl
{
    /// <summary>
    /// 动态中台调用接口默认实现类
    /// </summary>
    public class DefaultDynamicMiddlePlatformService : IDynamicMiddlePlatformService
    {
        private readonly IMiddlePlatformService platformService; // 中台组件
        private readonly IDynamicMiddlePlatformUrl platformUrl;  // 动态url组件 负载均衡选择

        /// <summary>
        /// 函数注入
        /// </summary>
        /// <param name="platformService"></param>
        /// <param name="platformUrl"></param>
        public DefaultDynamicMiddlePlatformService(IMiddlePlatformService platformService, IDynamicMiddlePlatformUrl platformUrl)
        {
            this.platformService = platformService;
            this.platformUrl = platformUrl;
        }

        public IList<IDictionary<string, object>> GetList(string urlScheme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
        {
            // 1、获取中台Url
            string url = this.platformUrl.GetMiddlePlatformUrl(urlScheme, serviceName);

            // 2、调用请求
            Task<MiddlePlatformResult> platformResult = this.platformService.Get(url + serviceLink, middleParam);

            // 3、判断是否调用成功
            IsSccuess(platformResult);

            return platformResult.Result.ResultList;
        }

        public IDictionary<string, object> Get(string urlScheme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
        {
            // 1、获取中台Url
            string url = this.platformUrl.GetMiddlePlatformUrl(urlScheme, serviceName);

            // 2、调用请求
            Task<MiddlePlatformResult> platformResult = this.platformService.Get(url + serviceLink, middleParam);

            // 3、判断是否调用成功
            IsSccuess(platformResult);

            return platformResult.Result.ResultDic;
        }

        public dynamic GetDynamic(string urlScheme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
        {
            // 1、获取中台Url
            string url = this.platformUrl.GetMiddlePlatformUrl(urlScheme, serviceName);

            // 2、调用请求
            Task<MiddlePlatformResult> platformResult = this.platformService.Get(url + serviceLink, middleParam);

            // 3、判断是否调用成功
            IsSccuess(platformResult);

            return platformResult.Result.ResultDic;
        }

        public IList<T> GetList<T>(string urlScheme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
            where T : new()
        {
            // 1、获取中台Url
            string url = this.platformUrl.GetMiddlePlatformUrl(urlScheme, serviceName);

            // 2、调用请求
            Task<MiddlePlatformResult> platformResult = this.platformService.Get(url + serviceLink, middleParam);

            // 3、判断是否调用成功
            IsSccuess(platformResult);

            return ConvertUtil.MiddleResultToList<T>(platformResult.Result);
        }


        /// <summary>
        /// Get请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="urlScheme"></param>
        /// <param name="serviceName"></param>
        /// <param name="serviceLink"></param>
        /// <param name="middleParam"></param>
        /// <returns></returns>
        public T Get<T>(string urlScheme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
            where T : new()
        {
            // 1、获取中台Url
            string url = this.platformUrl.GetMiddlePlatformUrl(urlScheme, serviceName);

            // 2、调用请求
            Task<MiddlePlatformResult> platformResult = this.platformService.Get(url + serviceLink, middleParam);

            // 3、判断是否调用成功
            IsSccuess(platformResult);

            return ConvertUtil.MiddleResultToObject<T>(platformResult.Result);
        }

        /// <summary>
        /// Post请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="urlScheme"></param>
        /// <param name="serviceName"></param>
        /// <param name="serviceLink"></param>
        /// <param name="middleParam"></param>
        /// <returns></returns>
        public void Post(string urlScheme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
        {
            // 1、获取中台Url
            string url = this.platformUrl.GetMiddlePlatformUrl(urlScheme, serviceName);

            // 2、调用请求
            Task<MiddlePlatformResult> platformResult = this.platformService.Post(url + serviceLink, middleParam);

            // 3、判断是否调用成功
            IsSccuess(platformResult);
        }

        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="urlScheme"></param>
        /// <param name="serviceName"></param>
        /// <param name="serviceLink"></param>
        /// <param name="middleParam"></param>
        public void Post(string urlScheme, string serviceName, string serviceLink, IList<IDictionary<string, object>> middleParam)
        {
            // 1、获取中台Url
            string url = this.platformUrl.GetMiddlePlatformUrl(urlScheme, serviceName);

            // 2、调用请求
            Task<MiddlePlatformResult> platformResult = this.platformService.Post(url + serviceLink, middleParam);

            // 3、判断是否调用成功
            IsSccuess(platformResult);
        }

        /// <summary>
        /// Post请求，动态参数
        /// </summary>
        /// <param name="urlScheme"></param>
        /// <param name="serviceName"></param>
        /// <param name="serviceLink"></param>
        /// <param name="middleParam"></param>
        /// <returns></returns>
        public dynamic PostDynamic(string urlScheme, string serviceName, string serviceLink, dynamic middleParam)
        {
            // 1、获取中台Url
            string url = this.platformUrl.GetMiddlePlatformUrl(urlScheme, serviceName);

            // 2、调用请求
            Task<MiddlePlatformResult> platformResult = this.platformService.PostDynamic(url + serviceLink, middleParam);

            // 3、判断是否调用成功
            IsSccuess(platformResult);

            return platformResult.Result.Result;
        }

        /// <summary>
        /// Delete请求
        /// </summary>
        /// <param name="urlScheme"></param>
        /// <param name="serviceName"></param>
        /// <param name="serviceLink"></param>
        /// <param name="middleParam"></param>
        public void Delete(string urlScheme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
        {
            // 1、获取中台Url
            string url = this.platformUrl.GetMiddlePlatformUrl(urlScheme, serviceName);

            // 2、调用请求
            Task<MiddlePlatformResult> platformResult = this.platformService.Delete(url + serviceLink, middleParam);

            // 3、判断是否调用成功
            IsSccuess(platformResult);
        }

        /// <summary>
        /// Delete请求
        /// </summary>
        /// <param name="urlScheme"></param>
        /// <param name="serviceName"></param>
        /// <param name="serviceLink"></param>
        /// <param name="middleParam"></param>
        public dynamic DeleteDynamic(string urlScheme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
        {
            // 1、获取中台Url
            string url = this.platformUrl.GetMiddlePlatformUrl(urlScheme, serviceName);

            // 2、调用请求
            Task<MiddlePlatformResult> platformResult = this.platformService.Delete(url + serviceLink, middleParam);

            // 3、判断是否调用成功
            IsSccuess(platformResult);

            return platformResult.Result.Result;
        }

        /// <summary>
        /// Put请求
        /// </summary>
        /// <param name="urlScheme"></param>
        /// <param name="serviceName"></param>
        /// <param name="serviceLink"></param>
        /// <param name="middleParam"></param>
        public void Put(string urlScheme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
        {
            // 1、获取中台Url
            string url = this.platformUrl.GetMiddlePlatformUrl(urlScheme, serviceName);

            // 2、调用请求
            Task<MiddlePlatformResult> platformResult = this.platformService.Put(url + serviceLink, middleParam);

            // 3、判断是否调用成功
            IsSccuess(platformResult);
        }

        /// <summary>
        /// Put请求，动态参数
        /// </summary>
        /// <param name="urlScheme"></param>
        /// <param name="serviceName"></param>
        /// <param name="serviceLink"></param>
        /// <param name="middleParam"></param>
        /// <returns></returns>
        public dynamic PutDynamic(string urlScheme, string serviceName, string serviceLink, dynamic middleParam)
        {
            // 1、获取中台Url
            string url = this.platformUrl.GetMiddlePlatformUrl(urlScheme, serviceName);

            // 2、调用请求
            Task<MiddlePlatformResult> platformResult = this.platformService.PutDynamic(url + serviceLink, middleParam);

            // 3、判断是否调用成功
            IsSccuess(platformResult);

            return platformResult.Result.Result;
        }

        /// <summary>
        /// Put请求
        /// </summary>
        /// <param name="urlScheme"></param>
        /// <param name="serviceName"></param>
        /// <param name="serviceLink"></param>
        /// <param name="middleParam"></param>
        public void Put(string urlScheme, string serviceName, string serviceLink, IList<IDictionary<string, object>> middleParam)
        {
            // 1、获取中台Url
            string url = this.platformUrl.GetMiddlePlatformUrl(urlScheme, serviceName);

            // 2、调用请求
            Task<MiddlePlatformResult> platformResult = this.platformService.Put(url + serviceLink, middleParam);

            // 3、判断是否调用成功
            IsSccuess(platformResult);
        }

        /// <summary>
        /// 是否调用成功
        /// </summary>
        /// <param name="platformResult"></param>
        private void IsSccuess(Task<MiddlePlatformResult> platformResult)
        {
            if (!"Success".Equals(platformResult.Result.Success))
                throw new ArgumentNullException($"中台调用异常：{platformResult.Result.ErrorInfo}");
        }
    }
}
